﻿using System.Collections;
using System.Collections.Generic;
using System;

class DiagMethod
{
    int n;
    double[] a, b, c;
    double[] fi;
    double k1, k2;
    double mu1, mu2;

    public DiagMethod(int n, double[] a, double[] b, double[] c, double[] fi, double k1, double k2, double mu1, double mu2)
    {
        this.n = n;
        this.a = a;
        this.b = b;
        this.c = c;
        this.fi = fi;
        this.k1 = k1;
        this.k2 = k2;
        this.mu1 = mu1;
        this.mu2 = mu2;
    }

    public double[] Solve()
    {
        double[] alpha = new double[n-1];
        double[] beta = new double[n-1];
        double[] x = new double[n];
        alpha[0] = k1;
        beta[0] = mu1;
        for (int i = 0; i < n-2; i++)
        {
            alpha[i + 1] = b[i] / (c[i] - alpha[i] * a[i]);
            beta[i + 1] = (fi[i] + beta[i] * a[i]) / (c[i] - alpha[i] * a[i]);
        }
        x[n-1] = (mu2+k2*beta[n-2])/(1-k2*alpha[n-2]);
        for(int i=n-2;i>=0;i--)
            x[i] = x[i+1]*alpha[i]+beta[i];
        return x;
    }
}